//
//  GoingProjectViewController.m
//  work
//
//  Created by 徐宏飞 on 16/5/27.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "GoingProjectViewController.h"

#import "CLWeeklyCalendarView.h"
#import "GoingProjectTableViewCell.h"
#import "MyReportViewController.h"     //上传
#import "FinishViewController.h"       //浏览
#import "GoingProjectModel.h"


static NSString *goingProjectTVC_ID = @"GoingProjectTableViewCell";

@interface GoingProjectViewController ()<CLWeeklyCalendarViewDelegate,
UITableViewDelegate,
UITableViewDataSource,
NetworkToolerDelegate
>

@property (nonatomic, strong) CLWeeklyCalendarView* calendarView;

@property (weak, nonatomic) IBOutlet UITableView *goingProjectTV;
@property (nonatomic, strong) NSMutableArray *goingProjectArray;
@property (nonatomic, assign) NSInteger loadpage;
@property (nonatomic, assign) NSInteger total;


@property (nonatomic, copy) NSMutableString *pickDay;

@property (nonatomic, strong) NetworkTooler *network;
@property (nonatomic, strong) ZhuangxiuManager *manager;



@end

@implementation GoingProjectViewController


#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"工作报告";
    _network  = [NetworkTooler new];
    _manager = [ZhuangxiuManager shareManager];
    _goingProjectArray = [NSMutableArray array];
    
    
    [self.view addSubview:self.calendarView];
    
    [_goingProjectTV registerNib:[UINib nibWithNibName:@"GoingProjectTableViewCell" bundle:nil] forCellReuseIdentifier:goingProjectTVC_ID];

    _goingProjectTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    __weak GoingProjectViewController *weakself = self;
    
    _goingProjectTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //        self.page = 0;
        
        [weakself updateNewData];
    }];
    
//    _goingProjectTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [weakself updateMoreData];
//    }];
    

    
    

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (_pickDay.length>0) {
        
        [self _postProjectDataWithDateNSString:_pickDay];
    }

    [MobClick beginLogPageView:@"WorkReport"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"WorkReport"];
}


#pragma mark - Data
- (void) _postProjectDataWithDateNSString:(NSString *)str{
    
    _network.delegate = self;
    
    NSDictionary *parameters = @{
                                 @"day":str,
                                 @"session_id":self.manager.sessionid,
                            
                                 };
    
    NSLog(@"上传参数:%@", parameters);
    
    [_network parameters:parameters withURL:getWorkingProjects_show  withIsPaparameters:YES];
    
    
    
    
}


#pragma mark - NetworkToolerDelegate
- (void)returnData:(id)data{
    
    [GiabHUD hideUIBlockingIndicator];
    [self goingProjectTVendRefresh];
    self.bgView.hidden = YES;
    
    if (isNSDictionary) {

        
            
            [self.goingProjectArray removeAllObjects];
        

        
        NSArray *array = [data objectForKey:@"data"];
        if (array.count==0) {
            [self.goingProjectTV reloadData];
            _total = 0;
            return;
        }
        _total =[[data objectForKey:@"total"] integerValue];

        
        for (NSMutableDictionary *dic in array) {
            GoingProjectModel *model = [GoingProjectModel modelWithDictionary:dic];
            [self.goingProjectArray addObject:model];
        }
        
        
        [self.goingProjectTV reloadData];
    
    }
  
}

- (void)returnRequestError:(NSError *)error{
    
    [GiabHUD hideUIBlockingIndicator];
    [self goingProjectTVendRefresh];
    
}

- (void)returnMsg:(NSString *)Msg{
    [GiabHUD hideUIBlockingIndicator];
    [self goingProjectTVendRefresh];
    
    if ([Msg isEqualToString:@"你的网络有问题哦!"]) {
        self.bgView.hidden = NO;
        [self.goingProjectArray removeAllObjects];
        [_goingProjectTV reloadData];
        return;
    }
    
    
    [self showAlertWithTitle:@"温馨提示" message:Msg cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
    
    
}






#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!self.enableReturnButton) {
        return 0;
        
    }
    return self.goingProjectArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GoingProjectModel *model = [self.goingProjectArray objectAtIndex:indexPath.row];
    GoingProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:goingProjectTVC_ID];
    cell.model = model;
    cell.lblProjectname.text = [NSString stringWithFormat:@"项目%ld",(long)indexPath.row+1];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    GoingProjectModel *model = [self.goingProjectArray objectAtIndex:indexPath.row];
    
    if (model.task_day_id.intValue == 2) {
        [self showAlertWithTitle:@"温馨提示" message:@"项目还没有进行您不能填写和查看!" cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        return;
    }
    
    if (model.task_day_id.intValue == 1) {
        //进入浏览
        FinishViewController *myReport = [FinishViewController new];
        myReport.pickDay = _pickDay;
        myReport.model = model;
        myReport.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:myReport animated:YES];
        
    } else {
        //进入上传
        MyReportViewController *myReport = [MyReportViewController new];
        myReport.pickDay = _pickDay;
        myReport.model = model;
        myReport.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:myReport animated:YES];
    }
    
    
    
    
    
}


#pragma mark - CustomDelegate
//CLWeeklyCalendarViewDelegate
-(NSDictionary *)CLCalendarBehaviorAttributes
{
    return @{
             CLCalendarWeekStartDay : @7,
             
             //Start Day of the week, from 1-7 Mon-Sun -- default 1
             //             CLCalendarDayTitleTextColor : [UIColor yellowColor],
             //             CLCalendarSelectedDatePrintColor : [UIColor greenColor],
             };
}

-(void)dailyCalendarViewDidSelect:(NSDate *)date
{
//    You can do any logic after the view select the date
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    _pickDay = [currentDateStr mutableCopy];
    _pickDay = [[_pickDay substringToIndex:10] mutableCopy];
    
    
    
    [self _postProjectDataWithDateNSString:_pickDay];

    
    
}






#pragma mark - private methods
-(void)updateNewData{
    
    _loadpage = 1;
    [self _postProjectDataWithDateNSString:_pickDay];
    
    
}

-(void)updateMoreData{
    
    _loadpage++;
    
    if (_loadpage <= (int)((_total/listConut)+1)) {
        
        [self _postProjectDataWithDateNSString:_pickDay];
    }else{
        [_goingProjectTV.mj_footer endRefreshingWithNoMoreData];
    }
}

-(void)goingProjectTVendRefresh{
    [_goingProjectTV.mj_header endRefreshing];
    [_goingProjectTV.mj_footer endRefreshing];
}










#pragma mark - gettsers and setters
//Initialize
-(CLWeeklyCalendarView *)calendarView
{
    if(!_calendarView){
        _calendarView = [[CLWeeklyCalendarView alloc] initWithFrame:CGRectMake(0, -20, SCREEN_WIDTH, 110)];
        _calendarView.delegate = self;
    }
    return _calendarView;
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
